//
//  ZMYUserInfoTableViewController.m
//  奇阅
//
//  Created by 奇阅科技 on 14-10-28.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ZMYUserInfoTableViewController.h"
#import "Config.h"
#import "ZMYLoginViewController.h"
#import "ZMYUpdateNickNameViewController.h"
#import "ZMYChangePasswdViewController.h"
@interface ZMYUserInfoTableViewController ()

@end

@implementation ZMYUserInfoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    self.navigationController.navigationBarHidden = NO;
    [self setBarBackgroundImage:@"navBackground"];
    [self setTitleForNavigation:@"个人中心"];
    [self setLeftBackButtonForNavigation];
    [self.headImageVIew addImageFullViewAnimation];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setInfoLabel];
}
-(void)setInfoLabel{
    UserAccount *user = [UserAccount currentAccount];
    if (user.uid.length>1) {
        if (user.headimg.length>1) {
            [self.headImageVIew setImageWithURL:[NSURL URLWithString:user.headimg]placeholderImage:[UIImage imageNamed:@"touxiang"]];
        } else {
            self.headImageVIew.image = [UIImage imageNamed:@"touxiang"];
        }
        self.userNameLabel.text = user.username;
        self.mobileLabel.text = user.mobile?user.mobile:@"未绑定";
        self.nickNameLabel.text = user.nickname;
    } else {
        self.headImageVIew.image = [UIImage imageNamed:@"touxiang"];
        self.userNameLabel.text = @"";
        self.mobileLabel.text = @"";
        self.nickNameLabel.text = @"";
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UserAccount *user = [UserAccount currentAccount];
    if (user.uid.length>1) {
        if (indexPath.section == 0) {
            UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
            [sheet setCancelButtonIndex:1];
            if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
                [sheet addButtonWithTitle:@"拍照"];
                [sheet setCancelButtonIndex:2];
            }
            [sheet addButtonWithTitle:@"从相册选择"];
            [sheet addButtonWithTitle:@"取消"];
            [sheet showFromRect:self.view.bounds inView:self.view animated:YES];
        } else if(indexPath.section == 1&&indexPath.row == 2){
            ZMYChangePasswdViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"ZMYChangePasswdViewController"];
            [self.navigationController pushViewController:view animated:YES];
        } else if(indexPath.section == 2){
            ZMYUpdateNickNameViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"ZMYUpdateNickNameViewController"];
            [self.navigationController pushViewController:view animated:YES];
        }else if(indexPath.section == 3){
            ZMYLoginViewController *view = [[UIStoryboard storyboardWithName:@"BookRack" bundle:nil]instantiateViewControllerWithIdentifier:@"ZMYLoginViewController"];
            view.isPush = YES;
            [self.navigationController pushViewController:view animated:YES];
        }else{
            return;
        }
    } else {
        [AppDelegate showHUDAndHide:@"请登录后继续操作" view:self.view];
        return;
    }
}
#pragma mark - UIActionSheetDelegate 拍照、相册选择器代理
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == actionSheet.cancelButtonIndex) {
        return;
    }
    NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"拍照"]) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }else if([title isEqualToString:@"从相册选择"]){
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    //得到图片
    UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self.navigationController setSGProgressPercentage:0.2 andTintColor:YellowColor];
    [[ZMYNetWorkAPI default]updateHeadImage:image response:^(NSDictionary *resultDic) {
        [self.navigationController setSGProgressPercentage:100];
        UserAccount *user = [UserAccount currentAccount];
        user.headimg = [resultDic objectForKey:@"headimg"];
        [UserAccount saveAccount:user];
        [self.headImageVIew setImageWithURL:[NSURL URLWithString:user.headimg]placeholderImage:[UIImage imageNamed:@"touxiang"]];
    } progress:^(CGFloat progres) {
        [self.navigationController setSGProgressPercentage:progres*100];
    } error:^(NSString *errorStr) {
        [self.navigationController cancelSGProgress];
        [AppDelegate showHUDAndHide:errorStr view:self.view];
    }];
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}
@end
